package com.medical.acc.service.impl;

import com.medical.common.paginator.domain.PageBounds;
import com.medical.common.paginator.domain.PageList;
import com.medical.acc.mapper.AccountMapper;
import com.medical.acc.mapper.AccountRecordMapper;
import com.medical.acc.model.Account;
import com.medical.acc.model.People;
import com.medical.acc.service.AccountService;
import com.medical.acc.service.PeopleService;
import com.medical.acc.web.viewmodel.AccountModel;
import com.medical.sys.mapper.UserMapper;
import com.medical.sys.model.User;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by Administrator on 2016/10/12.
 */
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private  AccountMapper accountMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PeopleService peopleService;
    @Autowired
    private AccountRecordMapper accountRecordMapper;

    public List<Account> selectAll(Boolean administrator) {
        return accountMapper.selectAll(administrator);
    }

    public PageList<Account> searchList(String query, Integer page, Integer pageSize, String sortColumn, String sortDir) {
        Map<String,Object> params = new HashMap<String,Object>();
        if(page==null){
            page=1;
        }
        if(pageSize==null){
            pageSize=20;
        }
        if(!StringUtils.isEmpty(sortColumn)){
            params.put("sortColumn", sortColumn);
        }else {
            params.put("sortColumn", "id");
        }
        if(!StringUtils.isEmpty(sortDir)){
            params.put("sortDir", sortDir);
        }else {
            params.put("sortDir", "asc");
        }
        if(!StringUtils.isEmpty(query)){
            query = "%"+query+"%";
        }else {
            query = "%%";
        }
        params.put("query", query);

        return (PageList<Account>) accountMapper.selectList(params, new PageBounds(page, pageSize));
    }

    public void postAccount(AccountModel accountModel) {
        if (accountModel!=null){
            if(StringUtils.isEmpty(accountModel.getId())){
                accountModel.setId(UUID.randomUUID().toString());
                accountModel.setUserId(accountModel.getId());
            }
            if(StringUtils.isEmpty(accountModel.getFullname())){
                throw new PeopleException("姓名不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getPhone())){
                throw new PeopleException("手机号码不能为空");
            }
            if(StringUtils.isEmpty(String.valueOf(accountModel.getLevelId()))){
                accountModel.setLevelId(0);
            }
            if(StringUtils.isEmpty(accountModel.getUsername())){
                throw new PeopleException("用户名不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getEmail())){
                throw new PeopleException("邮箱不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getPassword())){
                throw new PeopleException("密码不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getPayPassword())){
                throw new PeopleException("支付密码不能为空");
            }
            Account account=new Account();
            account.setId(accountModel.getId());
            account.setUserId(accountModel.getUserId());
            account.setLevelId(accountModel.getLevelId());
            account.setPayPassword(accountModel.getPayPassword());
            accountMapper.insert(account);

            User user=new User();
            user.setId(accountModel.getId());
            user.setUsername(accountModel.getUsername());
            user.setPassword(accountModel.getPassword());
            user.setEmail(accountModel.getEmail());
            user.setFullname(accountModel.getFullname());
            user.setPhone(accountModel.getPhone());
            userMapper.insert(user);

            if (accountModel.getNew()){
                People people = new People();
                people.setId(UUID.randomUUID().toString());
                people.setUserId(user.getId());
                people.setFullname(accountModel.getFullname());
                people.setPhone(accountModel.getPhone());
                people.setInputDate(new Date());
                peopleService.savePeople(people);
                peopleService.savePeopleOrganizationMap(people.getId(), accountModel.getOrganizationId());
                peopleService.saveUserPeopleMap(people.getUserId(), people.getId());
            }


        }else {
            throw new PeopleException("会员账户信息不能为空");
        }
    }

    public void patchAccount(AccountModel accountModel) {
        if (accountModel!=null){
            if(StringUtils.isEmpty(accountModel.getFullname())){
                throw new PeopleException("姓名不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getPhone())){
                throw new PeopleException("手机号码不能为空");
            }
            if(StringUtils.isEmpty(String.valueOf(accountModel.getLevelId()))){
                accountModel.setLevelId(0);
            }
            if(StringUtils.isEmpty(accountModel.getUsername())){
                throw new PeopleException("用户名不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getEmail())){
                throw new PeopleException("邮箱不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getPassword())){
                throw new PeopleException("密码不能为空");
            }
            if(StringUtils.isEmpty(accountModel.getPayPassword())){
                throw new PeopleException("支付密码不能为空");
            }
            Account account=new Account();
            account.setId(accountModel.getId());
            account.setUserId(accountModel.getId());
            account.setLevelId(accountModel.getLevelId());
            account.setPayPassword(accountModel.getPayPassword());
            accountMapper.updateByPrimaryKey(account);

            User user=new User();
            user.setId(accountModel.getUserId());
            user.setUsername(accountModel.getUsername());
            user.setPassword(accountModel.getPassword());
            user.setEmail(accountModel.getEmail());
            user.setFullname(accountModel.getFullname());
            user.setPhone(accountModel.getPhone());
            userMapper.updateByPrimaryKey(user);


        }else {
            throw new PeopleException("会员账户信息不能为空");
        }
    }

    public int deleteByPrimaryKey(String id) {
        if(StringUtils.isEmpty(id)){
            throw new PeopleException("不存在此id");
        }
        return accountMapper.deleteByPrimaryKey(id);
    }

    public Account selectByPrimaryKey(String id) {
        return accountMapper.selectByPrimaryKey(id);
    }

    public void patchRecordForRemark(String recordId, String remark) {
        Map<String,String> params = new HashMap<String,String>();
        if(StringUtils.isEmpty(recordId)) {
            throw new PeopleException("ID不能为空");
        }
        if(StringUtils.isEmpty(remark)){
            throw new PeopleException("remark不能为空");
        }
        params.put("id", recordId);
        params.put("remark", remark);
        accountRecordMapper.updateForRemark(params);
    }


}
